package com.example.myapplication
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView

class MyActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val constraintLayout = ConstraintLayout(this)
        setContentView(constraintLayout)

        // configure Button
        val button3 = Button(this)
        button3.id = View.generateViewId() // Generate unique ID
        button3.text = "click me"
        constraintLayout.addView(button3)

        // TextView
        val textView = TextView(this)
        textView.id = View.generateViewId() // Generate unique ID
        textView.text = "Hello World"
        constraintLayout.addView(textView)

        // ImageView
        val imageView2 = ImageView(this)
        imageView2.id = View.generateViewId() // Generate unique ID
        imageView2.setImageResource(R.drawable.flag) // Example image resource
        constraintLayout.addView(imageView2)

        // ConstraintSet to define layout constraints
        val set = ConstraintSet()
        set.clone(constraintLayout)

        // TextView
        set.connect(textView.id, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP)
        set.connect(textView.id, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM)
        set.connect(textView.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
        set.connect(textView.id, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)
        set.setVerticalBias(textView.id, 0.557f)
        set.setHorizontalBias(textView.id, 0.498f)

        // Button
        set.connect(button3.id, ConstraintSet.TOP, textView.id, ConstraintSet.BOTTOM)
        set.connect(button3.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
        set.connect(button3.id, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)
        set.connect(button3.id, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM)
        set.setVerticalBias(button3.id, 0.38f)
        set.setHorizontalBias(button3.id, 0.498f)

        // ImageView
        set.connect(imageView2.id, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP)
        set.connect(imageView2.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
        set.connect(imageView2.id, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)
        set.connect(imageView2.id, ConstraintSet.BOTTOM, button3.id, ConstraintSet.TOP)

        // Apply the constraints to the ConstraintLayout
        set.applyTo(constraintLayout)
    }
}

//        setContentView(R.layout.layout)
//
//        button3 = findViewById(R.id.button3)
//        textView = findViewById(R.id.textView)
//
//
//        button3.setOnClickListener {
//            textView.text = resources.getString(R.string.clicked)
//        }

